package com.itheima.service.impl;


import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Map<String, Object> getBusinessReportData() {
        /* reportDate: null,
                todayNewMember: 0,
                totalMember: 0,
                thisWeekNewMember: 0,
                thisMonthNewMember: 0,
                todayOrderNumber: 0,
                todayVisitsNumber: 0,
                thisWeekOrderNumber: 0,
                thisWeekVisitsNumber: 0,
                thisMonthOrderNumber: 0,
                thisMonthVisitsNumber: 0,
                hotSetMeal: [
                    {name: '套餐AA', setmeal_count: 200, proportion: 0.222},
                    {name: '套餐BB', setmeal_count: 200, proportion: 0.222}*/
        try {
            Map<String, Object> map = new HashMap<>();

            //获得当前日期
            String today = DateUtils.parseDate2String(new Date());
            map.put("reportDate", today);

            //获得本周一的日期  获取周一的时间 方便推算周一到现在的时间
            String thisweekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());

            //获得本月第一天的日期  获取本月第一天的时间 方便推算第一天到现在的时间
            String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

            //今日新增的会员
            Integer todayNewMember = memberMapper.findMemberCountByDate(today);
            map.put("todayNewMember",todayNewMember);

            //总会员数
            Integer totalMember = memberMapper.findMemberTotalCount();
            map.put("totalMember", totalMember);

            //本周新增会员  select * from t_member where create>=monday
            Integer thisWeekNewMember = memberMapper.findMemberCountAfterDate(thisweekMonday);
            map.put("thisWeekNewMember", thisWeekNewMember);

            //本月新增会员
            Integer thisMonthNewMember = memberMapper.findMemberCountAfterDate(firstDay4ThisMonth);
            map.put("thisMonthNewMember", thisMonthNewMember);

            //今日预约数
            Integer todayOrderNumber = orderMapper.findOrderCountByDate(today);
            map.put("todayOrderNumber", todayOrderNumber);

            //今日到诊数
            Integer todayVisitsNumber=orderMapper.findVisitsCountByDate(today);
            map.put("todayVisitsNumber",todayVisitsNumber);

            //本周预约数
            Integer thisweekOrderNumber= orderMapper.findOrderCountAfterDate(thisweekMonday);
            map.put("thisWeekOrderNumber",thisweekOrderNumber);

            //本周到诊数
            Integer thisweekVisitsNumber= orderMapper.findVisitsCountAfterDate(thisweekMonday);
            map.put("thisWeekVisitsNumber",thisweekVisitsNumber);

            //本月预约数
            Integer thisMonthOrderNumber = orderMapper.findOrderCountAfterDate(firstDay4ThisMonth);
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);

            //本月预约到诊数
            Integer thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate(firstDay4ThisMonth);
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

            //热门套餐
            List<Map> hotSetmeal = orderMapper.findHotSetmeal();
            map.put("hotSetmeal", hotSetmeal);

            BigDecimal totalProportion = new BigDecimal(0);
            for (Map map1 : hotSetmeal) {
                BigDecimal proportion = (BigDecimal) map1.get("proportion");
                totalProportion = totalProportion.add(proportion);
            }
            map.put("totalProportion",totalProportion);
            
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
